/**
 * 图片及文件上传插件
 */

layui.define(['jquery', 'layer'], function (exports) {

    var $ = layui.$,
        layer = layui.layer;
    var UploadFile = function () {
        this.config = {
            title: '图片上传',
            number: 1,
            size: 4096,
        }
    }
    UploadFile.prototype.set = function (options) {
        var _this = this;
        $.extend(true, _this.config, options);
        return _this;
    }
    /**
     * 上传图片
     * @param mode 1单图片2多图片
     * @param callback
     */
    UploadFile.prototype.image = function (mode, callback) {
        var _this = this;
        var title = _this.config.title;
        if (parent.layer) {
            var Layer = parent.layer;
        } else {
            var Layer = layer;
        }
        var client = 'home';
        if (typeof  UPLOAD_TYPE != 'undefined') {
            client = UPLOAD_TYPE;
        }
        var url = '/upload?mode=' + mode + "&size=" + _this.config.size + "&number=" + _this.config.number + "&client=" + client;
        Layer.open({
            type: 2,
            title: false,
            content: url,
            closeBtn: false,
            anim: 4,
            resize: false,
            scrollbar: false,
            area: ['660px', '470px'],
            btn: ['确定使用', '取消'],
            yes: function (index, layero) {
                var contentWindow = $(layero).find('iframe')[0].contentWindow;
                var document = contentWindow.document;
                var return_datas = document.getElementById('select_images').innerHTML;
                var images = return_datas.split('|');
                callback(images);
                Layer.close(index);
            }
        })
    }
    var upload = new UploadFile();
    exports('uploadFile', function (options) {
        return upload.set(options);
    })

})